package com.example.hsh.homefragment;

import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentTransactionBugFixHack;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.example.hsh.homefragment.fragment.ContentDetailFragment;
import com.example.hsh.homefragment.fragment.ContentListFragment;
import com.example.hsh.homefragment.fragment.MainFragment;
import com.example.hsh.homefragment.fragment.TopPanelFragment;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public class MainActivity extends AppCompatActivity implements MainFragment.MainFragmentCallBack,ContentListFragment.ContentListFragmentCallBack {

    private Toolbar toolbar;
    private Unbinder unbinder;
    private FragmentTransaction transaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        toolbar = (Toolbar) this.findViewById(R.id.toolBar);
//        setSupportActionBar(toolbar);
//
//        if (getSupportActionBar() != null) {
//            getSupportActionBar().setHomeButtonEnabled(true);
//            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//        }

        unbinder = ButterKnife.bind(this);

        transaction = getSupportFragmentManager().beginTransaction();
        if (savedInstanceState == null || getSupportFragmentManager().findFragmentById(R.id.fragment_container) == null) {
            transaction.add(R.id.fragment_container, MainFragment.newInstance(true), MainFragment.class.getName());
        }
    }

    //================== Fragment CallBack Start====================
    @Override
    public void addTopPanelFragment() {
        getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.v_fragment_enter, 0, 0, R.anim.v_fragment_exit)
                .add(R.id.fragment_container, TopPanelFragment.newInstance(),TopPanelFragment.class.getName())
                .addToBackStack(null).commit();
    }

    @Override
    public void addContentListFragment() {
        getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.v_fragment_enter, 0, 0, R.anim.v_fragment_exit)
                .add(R.id.fragment_container, ContentListFragment.newInstance(true, true), ContentListFragment.class.getName())
                .addToBackStack(null).commit();
    }

    @Override
    public void addContentDetailFragment() {
        getSupportFragmentManager().beginTransaction()
                .setCustomAnimations(R.anim.v_fragment_enter, 0, 0, R.anim.v_fragment_exit)
                .add(R.id.fragment_container, ContentDetailFragment.newInstance(), ContentDetailFragment.class.getName())
                .addToBackStack(null).commit();
    }

    //================== Fragment CallBack End====================


    @Override
    protected void onResumeFragments() {
        super.onResumeFragments();
        //防止在Activity生命周期中提交导致的异常
        if (transaction != null) {
            transaction.commit();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (unbinder != null) {
            unbinder.unbind();
        }
    }
}
